﻿using FairyGUI;
using System;

/// <summary>
/// 加入房间界面
/// </summary>
public class UIHome_JoinRoom : UIPopupWindow {

    /// <summary>
    /// 房间号控件
    /// </summary>
    class RoomIdCtrl
    {
        /// <summary>
        /// 数字控件
        /// </summary>
        class NumberCtrl
        {
            Transition[] _anims;
            int _num;

            public NumberCtrl(GComponent comp)
            {
                _num = -1;

                _anims = new Transition[10];
                for (int iAnim = 0; iAnim < 10; ++iAnim)
                {
                    _anims[iAnim] = comp.GetTransition("显示" + iAnim);
                }
            }

            public void ShowNumber(int num)
            {
                _num = num;
                _anims[num].Play();
            }

            public void Clear()
            {
                if (_num != -1)
                {
                    _anims[_num].PlayReverse();
                    _num = -1;
                }
            }
        }

        public const int NUM_BITS = 6;

        int _roomId;
        NumberCtrl[] numberCtrls;

        public RoomIdCtrl(GComponent comp)
        {
            _roomId = 0;
            numberCtrls = new NumberCtrl[NUM_BITS];
            for (var iComp = 1; iComp <= NUM_BITS; ++iComp)
            {
                numberCtrls[iComp - 1] = new NumberCtrl(comp.GetChild("数字" + iComp).asCom);
            }
        }

        public void Clear()
        {
            _roomId = 0;
            foreach (NumberCtrl numCtrl in numberCtrls)
            {
                numCtrl.Clear();
            }
        }

        public void SetNumber(int bit, int value)
        {
            _roomId += (value * (int)(Math.Pow(10, NUM_BITS - bit)));
            numberCtrls[bit - 1].ShowNumber(value);
        }

        public void DelNumber(int bit)
        {
            int value = (int)(Math.Pow(10, NUM_BITS - bit));
            _roomId -= value * ((_roomId / value) % 10);
            numberCtrls[bit - 1].Clear();
        }

        public int roomId
        {
            get
            {
                return _roomId;
            }
        }
    }

    /// <summary>
    /// 键盘控件
    /// </summary>
    class KeyboardCtrl
    {
        Action<int> _numKeyDownCallback;
        Action _delKeyDownCallback;
        Action _clearKeyDownCallback;

        public KeyboardCtrl(GComponent comp)
        {
            for (int iBtn = 0; iBtn < 10; ++iBtn)
            {
                GButton btn = comp.GetChild("数字" + iBtn).asButton;
                int value = iBtn;
                btn.onClick.Add(() => { _OnNumKeyDown(value); });
            }

            GButton delBtn = comp.GetChild("删除按钮").asButton;
            delBtn.onClick.Add(() => { _delKeyDownCallback(); });

            GButton clearBtn = comp.GetChild("重输按钮").asButton;
            clearBtn.onClick.Add(() => { _clearKeyDownCallback(); });
        }

        void _OnNumKeyDown(int value)
        {
            DebugInfo.Log("键盘数字 " + value + " 被按下");
            _numKeyDownCallback(value);
        }

        public void OnNumKeyDown(Action<int> callback)
        {
            _numKeyDownCallback = callback;
        }

        public void OnDelKeyDown(Action callback)
        {
            _delKeyDownCallback = callback;
        }

        public void OnClearKeyDown(Action callback)
        {
            _clearKeyDownCallback = callback;
        }
    }

    ///////////////////////////////////////////////////////////////////////////

    static UIHome_JoinRoom _inst;

    /// <summary>
    /// 房间号控件
    /// </summary>
    RoomIdCtrl _roomIdCtrl;
    /// <summary>
    /// 键盘控件
    /// </summary>
    KeyboardCtrl _keyboardCtrl;
    /// <summary>
    /// 数字位
    /// </summary>
    int _bit = 1;

    public UIHome_JoinRoom()
    {
        _inst = this;

        // 加载界面
        base.Init(UIPackage.CreateObject("主界面", "加入房间界面").asCom);

        // 初始化关闭按钮
        GButton closeBtn = uiComp.GetChild("关闭按钮").asButton;
        closeBtn.onClick.Add(() => {
            _Close();
        });

        // 初始化房间号控件
        _roomIdCtrl = new RoomIdCtrl(uiComp.GetChild("房间号码").asCom);
        _roomIdCtrl.Clear();

        // 初始化键盘
        _keyboardCtrl = new KeyboardCtrl(uiComp.GetChild("键盘").asCom);
        _keyboardCtrl.OnNumKeyDown(value => {
            AudioController.inst.PlayUIClickEffect();

            if (_bit > RoomIdCtrl.NUM_BITS)
            {
                return;
            }

            _roomIdCtrl.SetNumber(_bit, value);
            _bit += 1;
        });
        _keyboardCtrl.OnDelKeyDown(() => {
            AudioController.inst.PlayUIClickEffect();

            _bit -= 1;
            if (_bit < 1)
            {
                _bit = 1;
            }
            else
            {
                _roomIdCtrl.DelNumber(_bit);
            }
        });
        _keyboardCtrl.OnClearKeyDown(() => {
            AudioController.inst.PlayUIClickEffect();

            _bit = 1;
            _roomIdCtrl.Clear();
        });

        // 初始确定加入按钮
        GButton joinBtn = uiComp.GetChild("确定加入按钮").asButton;
        joinBtn.onClick.Add(() => {
            if (_bit >= RoomIdCtrl.NUM_BITS)
            {
                int roomId = _roomIdCtrl.roomId;

                DebugInfo.Log("正在加入房间：" + roomId);

                GameController.inst.EnqueueMessage(
                    new BaseProceduar.Message(
                        Commands.CMD_MOD_MSG,
                        GameCommands.CMD_JOIN_ROOM,
                        roomId)
                    );
            }
        });
    }

    void _Close()
    {
        DebugInfo.Log("关闭加入房间界面");

        AudioController.inst.PlayUIClickEffect();
        uiComp.RemoveFromParent();
        uiComp.Dispose();
        base.Close();
    }

    /// <summary>
    /// 弹出窗口
    /// </summary>
    public static void Popup()
    {
        DebugInfo.Log("打开加入房间界面");
        UIRootController.inst.PopupWindow(new UIHome_JoinRoom());
    }

    /// <summary>
    /// 关闭窗口
    /// </summary>
    public static void CloseWindow()
    {
        if (_inst != null)
        {
            _inst._Close();
        }
    }
}
